/**
 * File Info：com.mys.springboot.controller.system.I18nController.java
 * Created Date：2017年5月16日 下午3:41:12
 * Created User: mys
 */
package com.mys.springboot.controller.system;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.mys.springboot.config.base.BaseController;
import com.mys.springboot.config.base.BaseResult;

/**
 * @description
 * 
 * @author mys
 * @version 1.0
 *
 */
@Controller
@RequestMapping("/i18n/")
public class I18nController extends BaseController {

	@RequestMapping("{language}")
	@ResponseBody
	public BaseResult setI18n(@PathVariable("language") String language, 
								HttpServletRequest request,
								HttpServletResponse response) {
		LocaleResolver resolver = RequestContextUtils.getLocaleResolver(request);
		Locale locale = LocaleContextHolder.getLocale();

		if (language.equals("zh")) { // 简体中文
			locale = Locale.SIMPLIFIED_CHINESE;
		} else if (language.equals("en")) { // 美式英语
			locale = Locale.US;
		}
		resolver.setLocale(request, response, locale);
		return BaseResult.success();
	}
}
